﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRAdminUI.Payroll
{
    public partial class CreatePaySlip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                searchEmployeePayslipDiv.Visible = false;
                save.Visible = false;
                edit.Visible = false;
            }
            msgLabel.Visible = false;
        }
        protected void FindEmployee(object o, EventArgs e)
        {

            Code code = new Code();
            code.CodeName = txtEmployeeCode.Value;

            PopulateEmployeeInfo(code);
            PopupalteSalaryInfo(code);
            PopulateAttendanceInfo(code);

            save.Visible = true;
            edit.Visible = false;
            searchEmployeePayslipDiv.Visible = false;
        }

        private void PopulateAttendanceInfo(Code code)
        {
            try
            {
                List<Attendance> attendances = new List<Attendance>();
                AttendanceManager attendanceManager = new AttendanceManager();
                attendances = attendanceManager.GetEmployeeAttendanceInfo(code);
                int present = 0;
                int totaldaysOfMonths = attendances.Count;
                int absent = 0;
                int holiday = 0;
                int leave = 0;
                int late = 0;

                foreach (Attendance attendance in attendances)
                {
                    switch (attendance.Status)
                    {
                        case "ABSENT": absent++; break;
                        case "LATE": late++; break;
                        case "PRESENT": present++; break;
                        case "HOLIDAY": holiday++; break;
                        case "LEAVE": leave++; break;
                    }
                }

                txtDaysPresent.Text = present.ToString();
                txtMonthOfDays.Text = totaldaysOfMonths.ToString();
                txtAbsentDays.Text = absent.ToString();
                txtHoliday.Text = holiday.ToString();
                txtLeave.Text = leave.ToString();
                txtLate.Text = late.ToString();
            }
            catch (Exception ex)
            {
                msgLabel.Text = ex.Message;
                msgLbl.ForeColor = Color.Red;
                msgLabel.Visible = true;
            }
        }

        private void PopupalteSalaryInfo(Code code)
        {
            try
            {
                Salary salary = new Salary();
                SalaryManager salaryManager = new SalaryManager();
                salary = salaryManager.GetEmployeeSalaryStructure(code);

                txtBasic.Text = salary.Basic.ToString();
                txtMedical.Text = salary.Medical.ToString();
                txtTransport.Text = salary.Transport.ToString();
                txtMobile.Text = salary.Mobile.ToString();

                txtPF.Text = salary.PF.ToString();
                txtInputPF.Text = salary.PF.ToString();
                txtGross.Text = salary.Gross.ToString();
                txtNetSalary.Text = (salary.Gross - salary.PF).ToString();
                ViewState["gross"] = salary.Gross;
                ViewState["pf"] = salary.PF;
            }
            catch (Exception ex)
            {
                msgLabel.Text = ex.Message;
                msgLbl.ForeColor = Color.Red;
                msgLabel.Visible = true;
            }
        }

        private void PopulateEmployeeInfo(Code code)
        {
            try
            {
                EmployeeManager employeeManager = new EmployeeManager();
                ViewEmployee viewEmployee = new ViewEmployee();
                viewEmployee = employeeManager.SearchEmployeeByCode(code);
                lblEmpStatus.Text = viewEmployee.Employee.Status;
                txtEmpCode.Text = viewEmployee.Code.CodeName;
                txtEmpName.Text = viewEmployee.Employee.Name;
                txtEmpDegignation.Text = viewEmployee.Designation.Title;
                txtEmpProject.Text = viewEmployee.Project.ProjectName;
                txtDOJ.Text = viewEmployee.Employee.JoiningDate.ToShortDateString();
            }
            catch (Exception ex)
            {
                msgLabel.Text = ex.Message;
                msgLbl.ForeColor = Color.Red;
                msgLabel.Visible = true;
            }
        }

        protected void txtConveyance_TextChanged(object sender, EventArgs e)
        {
            try
            {


                int convence = Convert.ToInt32(txtConveyance.Text);
                ViewState["conv"] = convence;
                int net = Convert.ToInt32(ViewState["gross"]) + Convert.ToInt32(ViewState["conv"]) + Convert.ToInt32(ViewState["incen"]) - Convert.ToInt32(ViewState["deduct"]) - Convert.ToInt32(ViewState["pf"]);
                txtNetSalary.Text = net.ToString();
            }
            catch (Exception)
            {
                msgLbl.Text = "";
            }
            txtNetSalary.Focus();
        }

        protected void txtIncentive_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int incentive = Convert.ToInt32(txtIncentive.Text);
                ViewState["incen"] = incentive;
                int net = Convert.ToInt32(ViewState["gross"]) + Convert.ToInt32(ViewState["conv"]) + Convert.ToInt32(ViewState["incen"]) - Convert.ToInt32(ViewState["deduct"]) - Convert.ToInt32(ViewState["pf"]);
                txtNetSalary.Text = net.ToString();
            }
            catch (Exception)
            {
                msgLbl.Text = "";
            }
            txtNetSalary.Focus();
        }




        protected void txtDeduction_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int deduct = Convert.ToInt32(txtDeduction.Text);
                ViewState["deduct"] = deduct;
                int net = Convert.ToInt32(ViewState["gross"]) + Convert.ToInt32(ViewState["conv"]) + Convert.ToInt32(ViewState["incen"]) - Convert.ToInt32(ViewState["deduct"]) - Convert.ToInt32(ViewState["pf"]);
                txtNetSalary.Text = net.ToString();
            }
            catch (Exception)
            {
                msgLbl.Text = "";
            }
            txtNetSalary.Focus();
        }

        protected void searchPayslip(object sender, EventArgs e)
        {
            searchEmployeePayslipDiv.Visible = true;
            save.Visible = false;
            edit.Visible = true;
        }

        protected void SavePaySlip(object o, EventArgs e)
        {
            PaySlipManager manager = new PaySlipManager();

            try
            {
                if (lblEmpStatus.Text == "Active")
                {
                    PaySlip paySlip = new PaySlip();
                    Employee employee = new Employee();
                    Code code = new Code();
                    code.CodeName = txtEmpCode.Text;
                    employee.EmpCode = code;
                    paySlip.Employee = employee;
                    paySlip.Allowance = Convert.ToDecimal(txtConveyance.Text);
                    paySlip.Incentive = Convert.ToDecimal(txtIncentive.Text);
                    paySlip.PF = Convert.ToDecimal(txtInputPF.Text);
                    paySlip.Deduction = Convert.ToDecimal(txtDeduction.Text);
                    paySlip.NetPay = Convert.ToDecimal(txtNetSalary.Text);
                    paySlip.Month = selectMonth.Value;
                    paySlip.Year = selectYear.Value;
                    paySlip.CreateBy = Session["Code"].ToString();
                    paySlip.Date = DateTime.Now.ToShortDateString();
                    if (manager.SavePaySlip(paySlip))
                    {
                        msgLabel.Visible = true;
                        msgLbl.ForeColor = Color.Green;
                        msgLbl.Text = "Successfully Created Pay Slip for the month of " + paySlip.Month + " of Employee " + code.CodeName;
                    }
                    else
                    {
                        msgLabel.Visible = true;
                        msgLbl.ForeColor = Color.Red;
                        msgLbl.Text = "Pay Slip Allready Created for the month of " + paySlip.Month + " of Employee " + code.CodeName;
                    }
                }
                else
                {
                    msgLabel.Visible = true;
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "The Employee's present status is not ACTIVE !";
                }
            }
            catch (Exception ex)
            {
                msgLabel.Visible = true;
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        protected void FindPaySlip(object o, EventArgs e)
        {
            try
            {


                Code code = new Code();
                code.CodeName = txtEmployeeSearchCode.Value;

                PopulateEmployeeInfo(code);
                PopupalteSalaryInfo(code);
                PopulateAttendanceInfo(code);

                PaySlipManager paymanager = new PaySlipManager();
                PaySlip paySlip = new PaySlip();
                Employee employee = new Employee();
                employee.EmpCode = code;
                paySlip.Employee = employee;
                paySlip.Month = selectSearchMonth.Value;
                paySlip.Year = selectSearchYear.Value;
                paySlip = paymanager.GetPaySlip(paySlip);

                ViewState["payslipId"] = paySlip.Id;
                selectMonth.DataTextField = paySlip.Month;
                selectYear.DataTextField = paySlip.Year;
                txtConveyance.Text = paySlip.Allowance.ToString();
                txtIncentive.Text = paySlip.Incentive.ToString();
                txtInputPF.Text = paySlip.PF.ToString();
                txtDeduction.Text = paySlip.Deduction.ToString();
                txtNetSalary.Text = paySlip.NetPay.ToString();
                selectMonth.Value = paySlip.Month;
                selectYear.Value = paySlip.Year;
                ViewState["gross"] = txtGross.Text;
                ViewState["conv"] = txtConveyance.Text;
                ViewState["incen"] = txtIncentive.Text;
                ViewState["deduct"] = txtDeduction.Text;
                ViewState["pf"] = txtPF.Text;

            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
                msgLabel.Visible = true;
            }
        }


        protected void EditPaySlip(object sender, EventArgs e)
        {
            PaySlipManager manager = new PaySlipManager();
            try
            {
                PaySlip paySlip = new PaySlip();
                Employee employee = new Employee();
                Code code = new Code();
                code.CodeName = txtEmpCode.Text;
                employee.EmpCode = code;
                paySlip.Employee = employee;
                paySlip.Id = Convert.ToInt32(ViewState["payslipId"]);
                paySlip.Allowance = Convert.ToDecimal(txtConveyance.Text);
                paySlip.Incentive = Convert.ToDecimal(txtIncentive.Text);
                paySlip.PF = Convert.ToDecimal(txtInputPF.Text);
                paySlip.Deduction = Convert.ToDecimal(txtDeduction.Text);
                paySlip.NetPay = Convert.ToDecimal(txtNetSalary.Text);
                paySlip.Month = selectMonth.Value;
                paySlip.Year = selectYear.Value;
                paySlip.CreateBy = Session["Code"].ToString();
                paySlip.Date = DateTime.Now.ToShortDateString();
                if (manager.EditPaySlip(paySlip))
                {
                    msgLabel.Visible = true;
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Successfully Updated Pay Slip for the month of " + paySlip.Month + " of Employee " + code.CodeName;
                }
                else
                {
                    msgLabel.Visible = true;
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Pay Slip Allready Updated for the month of " + paySlip.Month + " of Employee " + code.CodeName;
                }
            }
            catch (Exception ex)
            {
                msgLabel.Visible = true;
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }

        }

        protected void ClearInfo()
        {
            txtEmpCode.Text = "";
            txtEmpName.Text = "";
            txtEmpDegignation.Text = "";
            txtEmpProject.Text = "";
            txtDOJ.Text = "";
            lblEmpStatus.Text = "";
            txtBasic.Text = "";
            txtMedical.Text = "";
            txtTransport.Text = "";
            txtMobile.Text = "";
            txtPF.Text = "";
            txtInputPF.Text = "";
            txtGross.Text = "";
            txtNetSalary.Text = "";
            txtConveyance.Text = "";
            txtIncentive.Text = "";
            txtDeduction.Text = "";
            txtDaysPresent.Text = "";
            txtMonthOfDays.Text = "";
            txtAbsentDays.Text = "";
            txtHoliday.Text = "";
            txtLeave.Text = "";
            txtLate.Text = "";


        }
    }
}